package io.geekidea.boot.overduequery.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.materialrule.entity.MaterialRule;
import io.geekidea.boot.materialrule.service.MaterialRuleService;
import io.geekidea.boot.overduequery.dto.OverdueQueryExcel;
import io.geekidea.boot.overduequery.entity.OverdueQuery;
import io.geekidea.boot.overduequery.query.OverdueQueryQuery;
import io.geekidea.boot.overduequery.service.OverdueQueryService;
import io.geekidea.boot.wlwarehouse.entity.WlWarehouse;
import io.geekidea.boot.wlwarehouse.service.IWlWarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/query")
@Tag(name = "超期物料查询")
public class OverdueQueryController {

    @Resource
    private OverdueQueryService overdueQueryService;

    @Resource
    private MaterialRuleService materialRuleService;

    @Resource
    private IWlWarehouseService iWlWarehouseService;


    @PostMapping("/list")
    @Operation(tags = "Post", summary = "超期物料查询列表")
    public ApiResult OverdueQueryList(@RequestBody OverdueQueryQuery overdueQueryQuery) {
        List<OverdueQuery> overdueQueries = overdueQueryService.overdueQueryList(overdueQueryQuery);
        int total = overdueQueryService.getTotal(overdueQueryQuery).getTotal();
        PageDto<OverdueQuery> pageDto = new PageDto<>(overdueQueries, total, overdueQueryQuery.getPageSize(), overdueQueryQuery.getPageIndex() + 1);
        return ApiResult.success(pageDto);
    }

    @PostMapping("/rule/list")
    @Operation(summary = "搜索超期物料列表")
    public ApiResult roleList() {

        List<MaterialRule> list = materialRuleService.list();
        List<Map<String, Object>> areas = new ArrayList<>();
        for (MaterialRule role : list) {
            String name = role.getRulesName();
            Integer id = role.getId();
            Map<String, Object> areaMap = new HashMap<>();
            areaMap.put("name", name);
            areaMap.put("id", id);
            areas.add(areaMap);
        }
        return ApiResult.success(areas);
    }

    @PostMapping("/wl/list")
    @Operation(summary = "搜索仓库列表")
    public ApiResult wuList() {

        List<WlWarehouse> list = iWlWarehouseService.list();
        List<Map<String, Object>> wllist = new ArrayList<>();
        for (WlWarehouse wlWarehouse : list) {
            String name = wlWarehouse.getName();
            Integer id = wlWarehouse.getId();
            Map<String, Object> wlMap = new HashMap<>();
            wlMap.put("name", name);
            wlMap.put("id", id);
            wllist.add(wlMap);
        }
        return ApiResult.success(wllist);
    }

    @PostMapping("/download")
    @Operation(tags = "Post", description = "下载文件", summary = "超期物料查询表下载")
    public void download(@RequestBody OverdueQueryQuery overdueQueryQuery, HttpServletResponse response) {
        //WarehousentryQuery warehousentryQuery = new WarehousentryQuery();

        List<OverdueQueryExcel> list = overdueQueryService.queryDownList(overdueQueryQuery);
        OutputStream outputStream = null;
        try {
            response.setCharacterEncoding("UTF-8");
            String excelName = URLEncoder.encode("超期物料查询表" + LocalDateTime.now(), "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + excelName + ".xlsx");
            // 以字节流的形式输出响应正文
            outputStream = response.getOutputStream();
            try (ExcelWriter excelWriter = EasyExcel.write(outputStream, OverdueQueryExcel.class).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
                excelWriter.write(list, writeSheet);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}
